package io.github.talelin.latticy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.github.talelin.latticy.mapper.GoodsMapper;
import io.github.talelin.latticy.mapper.ShopMapper;
import io.github.talelin.latticy.mapper.ShopStockMapper;
import io.github.talelin.latticy.mapper.UserMapper;
import io.github.talelin.latticy.model.UserDO;
import io.github.talelin.latticy.model.ztyDO.ShopStockDO;
import io.github.talelin.latticy.pojo.Goods;
import io.github.talelin.latticy.pojo.Shop;
import io.github.talelin.latticy.pojo.ShopStock;
import io.github.talelin.latticy.service.ShopStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ZTY
 * @since 2021-04-15
 */
@Service
public class ShopStockServiceImpl extends ServiceImpl<ShopStockMapper, ShopStock> implements ShopStockService {

    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    ShopMapper shopMapper;

    @Autowired
    UserMapper userMapper;


    @Override
    public List<ShopStockDO> findAll() {
        List<ShopStockDO> shopStockDOList = new ArrayList<>();
        List<ShopStock> shopStocks = baseMapper.selectList(null);
        List<Goods> goods = goodsMapper.selectList(null);
        List<Shop> shops = shopMapper.selectList(null);
        for (ShopStock shopStock : shopStocks) {
            ShopStockDO shopStockDO = new ShopStockDO();

            shopStockDO.setShopId(shopStock.getShopId());
            shopStockDO.setShopStockId(shopStock.getShopStockId());
            shopStockDO.setStockValue(shopStock.getStockValue());
            for (Goods good : goods) {
                if (good.getGoodsId().equals(shopStock.getGoodsId())){
                    shopStockDO.setGoodsName(good.getGoodsName());

                }
            }
            for (Shop shop : shops) {
                if (shop.getShopId().equals(shopStock.getShopId())){
                    shopStockDO.setShopName(shop.getShopName());
                }
            }
            shopStockDOList.add(shopStockDO);
        }
        return shopStockDOList;
    }

    @Override
    public List<ShopStockDO> selectByUserId(int userId) {
        List<ShopStockDO> shopStockDOList = new ArrayList<>();
        List<ShopStock> shopStocks = baseMapper.selectList(null);
        List<Goods> goods = goodsMapper.selectList(null);
        UserDO userDO = userMapper.selectById(userId);
        Shop tempShop = shopMapper.selectById(userDO.getDepartmentId());
        for (ShopStock shopStock : shopStocks) {
            ShopStockDO shopStockDO = new ShopStockDO();

            shopStockDO.setShopId(shopStock.getShopId());
            shopStockDO.setShopStockId(shopStock.getShopStockId());
            shopStockDO.setStockValue(shopStock.getStockValue());
            for (Goods good : goods) {
                if (good.getGoodsId().equals(shopStock.getGoodsId())){
                    shopStockDO.setGoodsName(good.getGoodsName());
                    shopStockDO.setGoodsPrice(good.getGoodsPrice());
                }
            }

                if (tempShop.getShopId().equals(shopStock.getShopId())){
                    shopStockDO.setShopName(tempShop.getShopName());
                }
            shopStockDOList.add(shopStockDO);
        }
        return shopStockDOList;
    }
}
